<?php

namespace App\Http\Controllers\User;

use App\Constants\ResponseArray;
use App\Http\Controllers\BaseController;
use App\Http\Services\SMSService;
use App\Http\Services\User\UserService;
use Illuminate\Http\Request;

class RegisterController extends BaseController
{

    public function register(Request $request)
    {
        $rules = [
            'mobile'        => 'required|string|unique:user,mobile',
            'password'      => 'required|confirmed|string|min:6',
            'code'          => 'required|string',
            'is_agree'      => 'nullable',
        ];
        $messages = [
            'mobile.unique'    => '该手机号码已注册.',
            'mobile.confirmed' => '两次输入的密码不一致.',
            'mobile.code'      => '请输入验证码',
        ];
        $data = $request->all();
        $result = $this->validator($data, $rules, $messages);
        if ($result)
        {
            return ResponseArray::parameterError($result, $rules);
        }

        $validate = SMSService::verifyValidateCode($data['mobile'],$data['code']);

        if (!$validate)
        {
            return ResponseArray::abortMessage('短信验证失败,请重试.');
        }

        UserService::register($request);

        return ResponseArray::response(true,'注册成功.');
    }
}
